package com.quanshi.shopping_category_service.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanshi.shopping_category_service.mapper.CategoryMapper;
import com.quanshi.shopping_common.entity.Category;
import com.quanshi.shopping_common.service.ICategoryService;
import com.quanshi.shopping_common.constant.RedisKey;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;


@DubboService
public class CategoryServiceImpl implements ICategoryService {


    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 同步数据库数据到redis
     * 思路：无论增删改
     * 1，先删除redis之前缓存的数据
     * 2，再将数据库的开启状态的数据同步redis
     */
    public void syncCategoryToRedis() {
        //删除所有的数据(删除key)
        redisTemplate.delete(RedisKey.CATEGORYS);

        //数据库：查询所有符合规则广告数据
        List<Category> categories = getCategory();

        //获取操作list集合的对象
        ListOperations<String,Category> los = redisTemplate.opsForList();

        //将广告数据存储到redis集合
        los.rightPushAll(RedisKey.CATEGORYS, categories);

    }

    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
        syncCategoryToRedis();
    }

    @Override
    public void update(Category category) {
        categoryMapper.updateById(category);
        syncCategoryToRedis();
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        categoryMapper.updateById(category);
        syncCategoryToRedis();
    }

    @Override
    public void delete(Long[] ids) {
        LambdaUpdateWrapper<Category> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Category::getId, ids);
        categoryMapper.delete(updateWrapper);
        syncCategoryToRedis();
    }

    @Override
    public Category findById(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public Page<Category> search(int page, int size) {
        return categoryMapper.selectPage(new Page<>(page, size), null);
    }

    @Override
    public List<Category> findAll() {

        //获取操作list集合的对象
        ListOperations<String,Category> los = redisTemplate.opsForList();
        //先从redis查询所有符合条件的数据
        List<Category> categoryList = los.range(RedisKey.CATEGORYS, 0, -1);

        if (CollUtil.isNotEmpty(categoryList)){
            return categoryList;
        }
        return getCategory();
    }

    public List<Category> getCategory(){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //查询已经启动广告
        queryWrapper.eq(Category::getStatus, 1);
        return categoryMapper.selectList(queryWrapper);
    }

}
